package com.controller;

import com.dto.LeaveRequestDto;
import com.model.LeaveRequest;
import com.service.LeaveRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/leave")
public class LeaveRequestController {
    private final LeaveRequestService leaveRequestService;

    @Autowired
    public LeaveRequestController(LeaveRequestService leaveRequestService) {
        this.leaveRequestService = leaveRequestService;
    }

    @PostMapping("/submit")
    public ResponseEntity<LeaveRequest> submitLeaveRequest(@RequestBody LeaveRequestDto leaveRequestDto) {
        LeaveRequest leaveRequest = leaveRequestService.createLeaveRequest(leaveRequestDto.getStudentId(), leaveRequestDto.getReason());
        return ResponseEntity.status(HttpStatus.CREATED).body(leaveRequest);
    }

    @GetMapping("/status/{id}")
    public ResponseEntity<LeaveRequest> getLeaveRequestStatus(@PathVariable Long id) {
        Optional<LeaveRequest> leaveRequest = leaveRequestService.getLeaveRequestById(id);
        if (leaveRequest.isPresent()) {
            return ResponseEntity.ok(leaveRequest.get());
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}


